import { Request, RequestHandler, Response } from 'express';
import {
  IGetUserRolesByIdReq,
  IGetUserRolesByUserIdReq,
  IAddUserRolesReq,
  IDeleteUserRolesReq,
} from '../../models/user-roles.model';
import * as UserRolesService from '../services/user-roles.service';

/**
 * Get active userRoles records
 *
 * @param req Express Request
 * @param res Express Response
 */
export const getUserRoleList: RequestHandler = async (req: Request, res: Response) => {
  try {
    const userRoleList = await UserRolesService.getUserRolesList();

    res.status(200).json({
      data: userRoleList,
      code: 2000,
    })
  } catch (error) {
    console.error('[userRoleList.controller][getUserRoles][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '获取所有角色用户列表失败！'
    });
  }
};

/**
 * Get userRoles record based on id provided
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const getUserRolesById: RequestHandler = async (req: IGetUserRolesByIdReq, res: Response) => {
  try {
    const userRoles = await UserRolesService.getUserRolesById(req.params.id);

    res.status(200).json({
      data: userRoles,
      code: 2000,
    })
  } catch (error) {
    console.error('[userRoleList.controller][getUserRolesById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '获取角色用户列表失败！'
    });
  }
};

/**
 * Get UserRoles record based on user id provided
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const getUserRolesByUserId: RequestHandler = async (req: IGetUserRolesByUserIdReq, res: Response) => {
    try {
      const userRoles = await UserRolesService.getUserRolesByUserId(req.params.userId);
  
      res.status(200).json({
        data: userRoles,
        code: 2000,
      })
    } catch (error) {
      console.error('[userRoleList.controller][getUserRolesById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
      res.status(500).json({
        message: '获取所有角色用户列表失败！'
      });
    }
  };

/**
 * Inserts a new userRoles record
 *
 * @param req Express Request
 * @param res Express Response
 */
export const addUserRoles: RequestHandler = async (req: IAddUserRolesReq, res: Response) => {
  try {
    const result = await UserRolesService.insertUserRoles(req.body);

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error('[userRoleList.controller][addUserRoles][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '添加角色用户失败！'
    });
  }
};

/**
 * deletes a userRoles
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const deleteUserRolesByUserId: RequestHandler = async (req: IDeleteUserRolesReq, res: Response) => {
  try {
    const result = await UserRolesService.deleteUserRoles(req.params.userId);

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error('[userRoleList.controller][deleteUserRolesById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '删除角色用户信息失败！'
    });
  }
};